package nc.ui.pub.filesystem.treetable;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;



public class Test extends JFrame{
	private class MyModel extends AbstractTreeTableModel{
		String[] clsName = new String[]{"col1","col2","col3","row"};
		public MyModel(TreeNode root) {
			super(root);
		}
		public void add(TreeNode newNode ,int row){
			
		}
		public int getColumnCount() {
			return clsName.length;
		}

		public String getColumnName(int columnIndex) {
			return clsName[columnIndex];
		}

		public Object getValueAt(Object node,int rowIndex, int columnIndex) {
			if(columnIndex == 2){
				return true;
			}
			if(columnIndex==3){
				
				return rowIndex+"";
			}
			return node.toString()+columnIndex;
		}

		public boolean isCellEditable(Object node, int rowIndex,int columnIndex) {
//			if(columnIndex == 0)
//				return false;
			return true;
		}

		public void setValueAt(Object value, Object node, int rowIndex, int columnIndex) {
			if(columnIndex == 2){
				return;
			}
			((MutableTreeNode)node).setUserObject(value);
		}
		public Class<?> getColumnClass(int columnIndex) {
			if(columnIndex ==2){
				return Boolean.class;
			}
			return super.getColumnClass(columnIndex);
		}

		
	}
	private class MyModel2 extends AbstractTreeTableModel{
		public MyModel2(TreeNode root) {
			super(root);
		}

		String[] clsName = new String[]{"col1","col2","col3"};
	

		public int getColumnCount() {
			return clsName.length;
		}

		public String getColumnName(int columnIndex) {
			return clsName[columnIndex];
		}

//		@Override
//		public Class getColumnClass(int column) {
//			if(column == 0){
//				return TreeTableModel.class;
//			}
//			return super.getColumnClass(column);
//		}

		public Object getValueAt(Object node, int rowIndex,int columnIndex) {
			return node.toString()+columnIndex;
		}

		public Object getChild(Object parent, int index) {
			
			return ((TreeNode)parent).getChildAt(index);
		}

		public int getChildCount(Object parent) {
			// TODO Auto-generated method stub
			return ((TreeNode)parent).getChildCount();
		}

		public boolean isCellEditable(Object node,  int rowIndex,int column) {
		
			return true;
		}

		public void setValueAt(Object value, Object node, int rowIndex, int column) {
			// TODO Auto-generated method stub
			((MutableTreeNode)node).setUserObject(value);
		}
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		UIManager.put("Table.selectionBackground", Color.red);
//		UIManager.put("Tree.textBackground", Color.blue);
		Test test = new Test();
		test.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		test.setSize(900, 900);
		test.setVisible(true);
		
	}

	public Test() throws HeadlessException {
		super();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,1));
		TreeNode root =(TreeNode) new JTree().getModel().getRoot();
		final MyModel model = new MyModel(root);
		final CTreeTable tt = new CTreeTable(model);
		
//		tt.setRowSorter(new TableRowSorter(tt.getModel()));
//		tt.setTreeTableRowSorterEnable(true);
//		tt.setRowSorter(new Tabl);
		tt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//		tt.setShowLineNum(false);
		tt.setRootVisible(true);
//		tt.setBackground(Color.yellow);
//		tt.setRowHeight(30);
//		tt.setRowHeight(1, 50);
		panel.add(new JScrollPane(tt));
		
//		TreeNode root1 =(TreeNode) new JTree().getModel().getRoot();
//		MyModel2 m2 = new MyModel2(root1);
//		JTreeTable t = new JTreeTable(m2);
//		t.setBackground(Color.green);
//		panel.add(new JScrollPane(t));
		JButton btn = new JButton("add");
		btn.addActionListener(new ActionListener(){
			private boolean showLineNum = false;
			public void actionPerformed(ActionEvent e) {
//				tt.setRowSorter(null);
//				tt.setShowLineNum(showLineNum);
//				showLineNum = !showLineNum;getLeadSelectionPath
				TreePath tp = tt.getSelectionPath();
//				TreePath tp1 = tt.getTree().getLeadSelectionPath();
				System.out.println("tree sel path is "+tp);
//				System.out.println("tree leading path is "+tp1);
				if(tp == null){
					
					return; 
				}
//		
				DefaultMutableTreeNode node = (DefaultMutableTreeNode)tp.getLastPathComponent();
//				model.removeNodeFromParent(node);
				tt.insertNodeIntoParent(new DefaultMutableTreeNode("new node"), node, 0);

			}
			
		});
		
		panel.add(btn);
		JTree tree = new JTree();
		tree.setCellRenderer(new TreeCellRenderer(){

			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
				JLabel lbl  =new JLabel(value.toString());
				lbl.setOpaque(true);
				if(selected)
					lbl.setBackground(Color.gray);
				return 	lbl;
			}
			
		});
//		tree.setBackground(Color.green);
		JTextArea ta = new JTextArea();
		JScrollPane sp =new JScrollPane(tree);
//		JButton btn1 = new JButton("dfa");
		JLabel lbl = new JLabel("ddd");
		sp.setColumnHeaderView(lbl);
		panel.add(sp);
//		
//		JTable table = new JTable(4,4);
//		panel.add(new JScrollPane(table));
		getContentPane().add(panel);
	}

}
